﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Configuration;
using System.Data.Linq.SqlClient;
using System.Data.SqlClient;
using System.Data;

namespace Cosmetic.Logic
{
    public class SaleAndSearchAdManager
    {
        /// <summary>
        /// 查寻广告。当ID小于0时表示查寻所有广告。否则查找对应的广告。
        /// </summary>
        /// <returns></returns>
        public static DataSet GetAllInformation(int id)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;
                if (id < 0)
                {
                    cmdStr = "select * from dbo.SaleAndSearchAdvertisement";
                }
                else
                {
                    cmdStr = "select * from dbo.SaleAndSearchAdvertisement where ID= " + id + "";
                }

                SqlCommand com = new SqlCommand(cmdStr, con);
                DataSet ds = new DataSet();
                SqlDataAdapter adap = new SqlDataAdapter(com);
                adap.Fill(ds);
                return ds;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 用户前台页面，获取广告图片。
        /// </summary>
        /// <param name="PictureSeat"></param>
        /// <returns></returns>
        public static DataTable GetThePicturePath(string PictureSeat)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;
                cmdStr = "select PicturePath,other1 from SaleAndSearchAdvertisement where PictureSeat='" + PictureSeat + "'";
                SqlCommand com = new SqlCommand(cmdStr, con);
                SqlDataAdapter adap = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                return ds.Tables[0];
            }
            finally
            {
                con.Close();
            }
        }


        /// <summary>
        /// 删除对应的广告。
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteInformation(int id)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "delete dbo.SaleAndSearchAdvertisement where ID= " + id + "";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 更新广告。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pictureSeat"></param>
        /// <param name="picturePath"></param>
        public static void UpdateInformation(int id, string pictureSeat, string picturePath,string other)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "UPDATE [SaleAndSearchAdvertisement]  SET [PictureSeat] = '" + pictureSeat + "' ,[PicturePath] = '" + picturePath + "' ,[other1] ='" + other + "'  WHERE ID=" + id + "";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 新增一个广告。
        /// </summary>
        /// <param name="pictureSeat"></param>
        /// <param name="picturePath"></param>
        public static void InsertInformation( string pictureSeat, string picturePath,string other1)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;
                cmdStr = "INSERT INTO [SaleAndSearchAdvertisement] ([PictureSeat]  ,[PicturePath]  ,[other1]  ,[other2])  VALUES ('" + pictureSeat + "','" + picturePath + "' ,'" + other1 + "' ,'')";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }


    }//end class
}
